/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.edu.unicen.twitter.suggest.frontend.controllers;

import ar.edu.unicen.twitter.suggest.backend.datasources.dao.TwitterDatasetDao;
import ar.edu.unicen.twitter.suggest.backend.datasources.twitterservices.TwitterClient;
import ar.edu.unicen.twitter.suggest.backend.dtos.RecommendationReport;
import ar.edu.unicen.twitter.suggest.backend.dtos.UsuarioDataset;
import ar.edu.unicen.twitter.suggest.backend.dtos.UsuarioProxy;
import ar.edu.unicen.twitter.suggest.backend.services.RecommendationControllerInterface;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import twitter4j.auth.RequestToken;

/**
 *
 * @author Alex
 */
@Controller("configurationController")
public class ConfigurationController {

    @Autowired
    TwitterClient twitter;
    @Autowired
    TwitterDatasetDao twitterDatasetDao;
    private RequestToken token;
    private String pin;

    private void getToken() {
        token = twitter.getRequestToken();
    }

    public void refresh() {
        getToken();
    }

    public void add() {
        if (token != null && getPin() != null) {
            twitter.addTwitter(token, getPin());
        }
    }

    public String getPin() {
        return pin;
    }

    public void setPin(String pin) {
        this.pin = pin;
    }

    public String getUrl() {
        getToken();
        return token.getAuthorizationURL();
    }

    private static int[] getArray(List<Integer> list) {

        if (list != null) {
            int[] array = new int[list.size()];
            int f = 0;
            for (int id : list) {
                array[f] = id;
                ++f;
            }
            return array;
        } else {
            return new int[0];
        }
    }

    public void loadDataset() {
        BufferedReader data_to = null;
        BufferedReader data_from = null;
        try {
            String path_to = "D:/tesis/twitter/to/out.twitter";
            String path_from = "D:/tesis/twitter/from/out.twitter";
            File file_to = new File(path_to);
            File file_from = new File(path_from);
            data_to = new BufferedReader(new FileReader(file_to));
            data_from = new BufferedReader(new FileReader(file_from));
            String line_to;
            String line_from;

            int userCount = 0;

            int id_to_next = 1;
            int id_to_current = 1;
            int id_from_next = 1;
            int id_from_current = 1;
            long line_count_to = 0;
            long line_count_from = 0;

            line_to = data_to.readLine();
            line_from = data_from.readLine();
            long line_skip_to = 965874547;
            long line_skip_from = 1468321435;
            while (line_to != null && line_count_to < line_skip_to) {
                 if (line_count_to % 100000 == 0) {
                    System.out.println("[" + line_count_to + "] " + line_to);
                }
                line_to = data_to.readLine();
                ++line_count_to;
            }
            while (line_from != null && line_count_from < line_skip_from) {
                if (line_count_from % 100000 == 0) {
                    System.out.println("[" + line_count_from + "] " + line_from);
                }
                line_from = data_from.readLine();
                ++line_count_from;
            }
            Map<Integer, ArrayList<Integer>> follows = new HashMap<Integer, ArrayList<Integer>>();
            Map<Integer, ArrayList<Integer>> followers = new HashMap<Integer, ArrayList<Integer>>();
            Set<Integer> userIds = new HashSet<Integer>();
            long net_size = 0;
            while (line_to != null || line_from != null) {
                id_to_current = id_to_next;
                userIds.add(id_to_current);
                id_from_current = id_from_next;
                userIds.add(id_from_current);
                ArrayList<Integer> followList = follows.get(id_to_current);
                if (followList == null) {
                    followList = new ArrayList<Integer>();
                    follows.put(id_to_current, followList);
                } else {
                    net_size -= followList.size();
                }
                ArrayList<Integer> followerList = followers.get(id_from_current);
                if (followerList == null) {
                    followerList = new ArrayList<Integer>();
                    followers.put(id_from_current, followerList);
                } else {
                    net_size -= followerList.size();
                }
               // if (id_to_current <= id_from_current) {
                    while (line_to != null && id_to_next == id_to_current) {
                        String[] tokens = line_to.split(" ");
                        if (tokens[0].startsWith("%")) {
                            line_to = data_to.readLine();
                            ++line_count_to;

                        } else {
                            id_to_next = Integer.parseInt(tokens[0]);
                            int followId = Integer.parseInt(tokens[1]);
                            if (id_to_current == -1) {
                                id_to_current = id_to_next;
                            }
                            if (id_to_current == id_to_next) {
                                followList.add(followId);
                                line_to = data_to.readLine();
                                ++line_count_to;
                            } else {
//                                if (userCount % 100 == 0) {
//                                    System.out.println("TO[" + line_count_to + "] " + line_to);
//                                }
                                net_size += followList.size();
                            }

                        }
                    }
              //  }
             //   if (id_from_current <= id_to_current) {
                    while (line_from != null && id_from_next == id_from_current) {
                        //if (line_count_from % 100000 == 0) {
                        //    System.out.println("[" + line_count_from + "] " + line_from);
                        //}
                        String[] tokens = line_from.split(" ");
                        if (tokens[0].startsWith("%")) {
                            line_from = data_from.readLine();
                            ++line_count_from;
                        } else {
                            id_from_next = Integer.parseInt(tokens[1]);
                            int followerId = Integer.parseInt(tokens[0]);
                            if (id_from_current == -1) {
                                id_from_current = id_from_next;
                            }
                            if (id_from_current == id_from_next) {
                                followerList.add(followerId);
                                line_from = data_from.readLine();
                                ++line_count_from;
                            } else {

//                                if (userCount % 100 == 0) {
//                                    System.out.println("FROM[" + line_count_from + "] " + line_from);
//                                }
                                net_size += followerList.size();
                            }
                        }
                    }
             //   }
                if (net_size >= 100000) {
                    List<UsuarioDataset> users = new ArrayList<UsuarioDataset>();
                    for (Integer id : userIds) {
                        UsuarioDataset user = new UsuarioDataset(id);
                        user.setFollowsShortIds(getArray(follows.remove(id)));
                        user.setFollowersShortIds(getArray(followers.remove(id)));
                        ++userCount;
                        users.add(user);
                    }
                    userIds.clear();
                    twitterDatasetDao.merge(users);
                    net_size = 0;
                    System.out.println("USER[" + userCount + "] " + "(id_to: " + id_to_current + "[" + line_count_to + "], id_From: " + id_from_current + "[" + line_count_from + "])");
                }

            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ConfigurationController.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ConfigurationController.class
                    .getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                data_to.close();
                data_from.close();

            } catch (IOException ex) {
                Logger.getLogger(ConfigurationController.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
